﻿package net.wangit.view.unit.grid;

import java.util.ArrayList;
import java.util.List;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.view.element.ElementComponent;


public class Group {

	private String[] config;
	private int column;
	private boolean visible;
	private String attribute;
	private List trs;
	String rowId;

	public Group(String config[], int column) {
		this.column = 0;
		visible = true;
		trs = new ArrayList();
		this.config = config;
		rowId = config != null ? config[0] : null;
		this.column = column;
	}

	public void write(ActionContext ac, StringBuffer html, Row row,
			int rowIndex, String rowCss) {
		for (int i = 0; i < trs.size(); i++) {
			Tr tr = (Tr) trs.get(i);
			tr.write(ac, html, row, rowIndex, rowCss);
		}

	}

	public void writeListHead(ActionContext ac, StringBuffer html,
			String headCss) {
		for (int i = 0; i < trs.size(); i++) {
			Tr gridRow = (Tr) trs.get(i);
			gridRow.writeListHead(ac, html, headCss);
		}

	}

	public void addElement(ElementComponent element) {
		int rowSpan = element.getTdRowSpan();
		int colSpan = element.getTdColSpan();
		int rowIndex = getRowIndex(colSpan);
		Tr tr = (Tr) trs.get(rowIndex);
		tr.addElement(element);
		if (rowSpan > 1 && colSpan > 0) {
			for (int i = 1; i < rowSpan; i++)
				getTr(rowIndex + i).addColumn(colSpan);

		}
	}

	public void addColumn(ElementComponent element) {
		if (trs.size() > 0) {
			Tr tr = (Tr) trs.get(0);
			tr.addColumn(element);
		}
	}

	private Tr getTr(int rowIndex) {
		if (rowIndex >= trs.size()) {
			Tr tr = new Tr(this);
			trs.add(tr);
			return tr;
		} else {
			return (Tr) trs.get(rowIndex);
		}
	}

	private int getRowIndex(int colSpan) {
		if (trs.size() == 0) {
			trs.add(new Tr(this));
			return 0;
		}
		if (column <= 0)
			return 0;
		for (int i = 0; i < trs.size(); i++) {
			Tr tr = (Tr) trs.get(i);
			if (colSpan == 0 && tr.isFull() && i < trs.size() - 1
					&& ((Tr) trs.get(i + 1)).isEmpty() || !tr.isFull())
				return i;
		}

		if (colSpan > 0)
			trs.add(new Tr(this));
		return trs.size() - 1;
	}

	public int getRowSpan() {
		return trs.size();
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public int getColumn() {
		return column;
	}

	public String getRowId() {
		return rowId;
	}

	public void setRowId(String rowId) {
		this.rowId = rowId;
	}

	public String getAttribute() {
		return attribute;
	}

	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}

	public int size() {
		return trs.size();
	}

	public String getGroupId() {
		return config != null ? config[0] : null;
	}

	public String getGroupName() {
		return config != null ? config[1] : null;
	}

	public String getGroupAttribute() {
		return config != null && config.length >= 3 ? config[2] : null;
	}
}
